package com.woniuxy.springbootpro.controller;

import com.woniuxy.springbootpro.entity.User;
import com.woniuxy.springbootpro.service.UserService;
import com.woniuxy.springbootpro.utile.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api("用户控制器")
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("用户登录")
    @RequestMapping("/login")
    public void userLoging(User user, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User loginUser = userService.findUserByNameAndPwd(user);
        if(loginUser != null){
            Cookie cookie = new Cookie("token", String.valueOf(loginUser.getUid()));
            cookie.setPath("/");
            response.addCookie(cookie);
            request.getRequestDispatcher("/index.html").forward(request,response);
            return;
        }else {
            response.sendRedirect("/login.html");
            return;
        }
    }

    @ApiOperation("查询用户")
    @ResponseBody
    @PostMapping("/find/{id}")
    public Result findById(@ApiParam("查询的用户对象") @PathVariable("id") Integer id){
        User user = userService.findById(id);
        if(user != null){
            return Result.ok().data("findUser",user);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("查询所有用户")
    @ResponseBody
    @GetMapping("/findAll")
    public Result findAll(){
        List<User> list = userService.findAll();
        if(list != null){
            return Result.ok().data("findAll",list);
        }else {
            return Result.error();
        }
    }

    @ApiOperation("保存用户")
    @ResponseBody
    @PostMapping("/addUser")
    public Result addUser(@ApiParam("要被保存的用户对象") @RequestBody User user){
        boolean flag = userService.add(user);
        return flag?Result.ok():Result.error();
    }

    @ApiOperation("删除一位用户")
    @ResponseBody
    @PostMapping("/delete/{id}")
    public Result delete(@ApiParam("删除用户") @PathVariable("id") Integer id){
        boolean flag = userService.delete(id);
        return flag?Result.ok():Result.error();
    }

    @ApiOperation("修改用户")
    @ResponseBody
    @PostMapping("/updata")
    public Result updata(@ApiParam("修改用户") @RequestBody User user){
        boolean flag = userService.updata(user);
        return flag?Result.ok():Result.error();
    }
}
